import { observable, action, toJS } from 'mobx';
import * as  Apis from '../apis/govern';
import { ListStore } from './listStore';
import { CommonStore } from "./commonStore";
import { WeaTableNew, WeaForm } from 'comsMobx';
import { message, Modal, Button } from "antd";
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
export default class TaskAccountStore extends ListStore {


    @observable treeData = [];
    @observable prjList = [];
    @observable selectedTreeKey = 0;//左侧树key值,为0代表初始化//19.2.22开始给0 lusx
    @observable currentKey;//当前选中的事项id
    @observable currentData = {};//当前选中事项参数集合
    @observable loading = false;
    @observable prjListloading = false;
    @observable commonStore = new CommonStore();
    @observable searchValue = "";//搜索值
    @observable showMid = true
    @observable selectTabKey = "2";//tab选中值
    @observable title = '';
    @observable pagination = {
        pageSize: 10,
        current: 1,
        total: 0
    }
    @observable isSetCurrentKey = false;
    @observable hasTaskRight = false;
    firstComing = 0;
    mindComing = 0;
    @observable baseSearchValue = '';
    @observable isTreeShow = true;//督办类型树是否默认展示

    constructor(props) {
        super(props);
    }


    @action
    initTreeData() {
        Apis.getTreeDatas().then(data => {
            if (parseInt(data.status) == 1) {
                this.treeData = data.categotyData;
            } else {
                message.error(data.error);
            }
        }).catch(error => {
            message.error(error);
        });
    }

    @action
    getIsTreeShow() {
        Apis.getIsTreeShow().then(data => {
            if (parseInt(data.status) == 1) {
                this.isTreeShow = data.isTreeShow;
            } else {
                message.error(data.error);
            }
        }).catch(error => {
            message.error(error);
        });
    }

    //左侧树的点击事件
    @action
    setSelectedTreeKey = (key) => {
        if (key != this.selectedTreeKey) {
            this.form.resetForm();
            this.selectedTreeKey = key;
            this.initList("tree");
        }
    }
    @action
    setDoAgain(bool) {
        this.noDoAgain = bool;
    }

    @action
    clear() {
        this.selectedTreeKey = 0;
    }

    @action
    onCollapse = (side, bool) => {
        if (side == "left") {
            this.isTreeShow = bool;
        } else {
            this.showMid = bool;
        }
    }

    @action
    getProjectList = (params = { pageIndex: this.pagination.current, pageSize: this.pagination.pageSize }) => {
        this.prjListloading = true;
        let list = [];
        this.isSetCurrentKey = false;
        this.baseSearchValue = '';
       
        Apis.getTaskAccount({ ...params, categoryid: this.selectedTreeKey, "getPages": "1" }).then(data => {
            this.prjList = data.prjList;
            this.pagination.total = data.totalSize;
            if (this.prjList.length > 0) {
                this.currentKey = this.prjList[0].prjid;
                this.title = this.prjList[0].name;
                this.currentData = this.prjList[0];
            } else {
                this.title = '';
                this.currentData = {};
                this.currentKey = '';
            }
            this.isSetCurrentKey = true;
            this.prjListloading = false;
        });
    }

    @action
    getCurrentPageDatas = (current = this.pagination.current, pageSize = this.pagination.pageSize) => {
        this.getProjectList({
            pageIndex: current,
            pageSize: pageSize,
            prjname: this.searchValue
        });
    }

    /**
     * 翻页
     */
    @action
    onChangePaginNation = (current = this.pagination.current, pageSize = this.pagination.pageSize) => {
        this.pagination.current = current;
        this.pagination.pageSize = pageSize;
        this.initParam();
        this.getCurrentPageDatas();
    }

    /**
     * 搜索回调
     */
    @action
    onSearchListItem = (value) => {
        this.searchValue = value;
        this.pagination.current = 1;
        this.getCurrentPageDatas();
        // this.pagination.total = list.length;
    }

    /**
     * 搜索值发生变化时的回调
     */
    @action
    onSearchChange = (value) => {
        this.searchValue = value;
    }

    /**
     * 点击督办事项触发的事件
     */
    @action
    changeSelectedKey = (params) => {
        this.setPrjid(params)
    }


    @action
    setPrjid = (params) => {
        this.currentData = params;
        this.currentKey = params.prjid;
        this.title = params.name;
        this.initParam();
    }

    @action
    initList = (params) => {
        this.initParam();
        if('tree'!=params){
            this.initTreeParam();
        }
        this.pagination.current = 1;
        this.initTreeData();
        this.getProjectList();
        this.isSetCurrentKey = false;
    }

    @action
    tabOnChange = (e) => {
        if (e == '0') {
            this.firstComing = this.firstComing + 1;//每进来一次记载一次
        }
        if (e == '1') {//脑图在第一次进来的时候才请求后台提高效率
            this.mindComing = this.mindComing + 1;
        }
        this.selectTabKey = e;
    }

    @action
    lookDetail = () => {
        window.open("/spa/govern/static/index.html#/main/govern/proinfo?id=" + this.currentKey + "&type=" + 0)
    }

    /**
     * 初始化参数
     */
    initParam = (params) => {
        this.selectTabKey = '2';
        this.firstComing = 0;
        this.mindComing = 0;
    }

    initTreeParam = (params) =>{
        this.selectedTreeKey = 0;
        this.showMid = true;
    }

    getTabData = () => {
        let tabInfo = [];
        if (this.currentKey == '') {
            tabInfo = [
                {
                    title: getLabel(508060, '任务列表'),
                    viewcondition: '2',
                }
            ];
        } else {
            tabInfo = [
                {
                    title: getLabel(508059, '进度视图'),
                    viewcondition: '1',
                },
                {
                    title: getLabel(508060, '任务列表'),
                    viewcondition: '2',
                }
            ];
            if (this.hasTaskRight) {
                tabInfo = _.concat([{ title: getLabel(508061, '事项信息'), viewcondition: '0' }], tabInfo)
            }
        }


        return tabInfo;
    }

    getRightMenu = () => {
        let btns = [];
        if (this.currentKey != '' && this.selectTabKey == "0") {
            btns.push({ key: "look", icon: <i className='icon-coms-Journal' />, content: getLabel("83110", "查看详情"), onClick: () => this.lookDetail() });
        }
        return btns;
    }

    getButtons = () => {
        let btnArr = [];
        if (this.currentKey != '' && this.selectTabKey == "0") {
            btnArr.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@utjbmw@detail`} type="primary" onClick={() => this.lookDetail()}>{getLabel("83110", "查看详情")}</Button>)
        }
        return btnArr;
    }
}
